package com.example.servletdemo.servlet.listner.attribute;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/AttributeRemoveServlet")
public class AttributeRemoveServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //HttpServletRequest属性的移除
        req.removeAttribute("username");

        //ServletContext属性的设置
        ServletContext servletContext = req.getServletContext();
        servletContext.removeAttribute("username");

        //HttpSession属性的设置
        HttpSession session = req.getSession();
        session.removeAttribute("username");
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("session username被移除了 :"+session.getAttribute("username"));
        resp.getWriter().write("servletContext username被移除了 :"+servletContext.getAttribute("username"));
        resp.getWriter().write("HttpServletRequest username被移除了 :"+req.getAttribute("username"));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
